<div class="container-fluid">
    <!-- Page Heading -->
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">
                Checklist Jawaban</small>
            </h1>
            <ol class="breadcrumb">
                <li class="active">
                    <i class="fa fa-dashboard"></i> QAS Terjemah Al Qur'an Bahasa Indonesia
                </li>
            </ol>
        </div>
    </div>
    <!-- /.row -->
    
    <div class="row">
        <div class="my-content">
            <!-- CONTENT -->
            <form class="form-horizontal" method="post" action="">
                <div class="control-group" style="width: 100%;">
                    <label class="control-label" for="pertanyaan">Masukkan Kata</label>
                    <div class="controls" style="display: inline-block; margin-left: 10px; width:80%;">
                        <input style="width: 70%; border-radius: 10px; vertical-align: middle; padding-left: 14px; padding-right: 14px; background-color: #ffffff;
                border: 1px solid #cccccc; box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset; transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s; font-size: 14px;
                height: 35px; line-height: 20px;" type="text" id="pertanyaan" name="keyword" placeholder="neraka" class="input-xxlarge search-query">
                        <button type="submit" name="search" class="btn" style="-moz-border-bottom-colors: none; -moz-border-left-colors: none;
                -moz-border-right-colors: none; -moz-border-top-colors: none; background-color: #f5f5f5; background-image: linear-gradient(to bottom, #ffffff, #e6e6e6);
                background-repeat: repeat-x; border-color: rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.25); border-image: none; border-radius: 15;
                border-style: solid; border-width: 1px; box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05); color: #333333; cursor: pointer; display: inline-block; font-size: 14px; line-height: 20px; margin-bottom: 0; text-align: center; text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
                vertical-align: middle;">Search</button>
                    </div>
                </div>
            </form>
            <br/>

            <?php 

            if (!isset($_SESSION['keyword'])) {
                $_SESSION['keyword'] = '';
            }

            if (isset($_POST['vote'])) {
                if (!empty($_POST['list-checkbox'])) {
                    $listCheckbox = explode("&", $_POST['list-checkbox']);
                    // $dataCheckbox = array();
                    $jumlahCheck  = 0;
                    // $jumlahData   = 0;
                    // $jumlahDataReal = 10;
                    $detailVote = array();
                    foreach ($listCheckbox as $data) {
                        $jumlahData++;
                        $nilai = 0;
                        if (isset($_POST[$data])) {
                            $nilai = 1;
                            $jumlahCheck++;
                            $detailVote[$data] = $data;
                        }
                    }
                    $detailHasil = implode("&", $detailVote);

                    $tanggal = new \DateTime;
                    $sql = "INSERT INTO `list_jawaban` (`tanggal` ,`keyword` ,`pertanyaan` ,`hasil` ,`jumlah_data`) VALUES (:tanggal, :keyword, :pertanyaan, :hasil, :jumlah_data)";
                    $q   = $pdo->prepare($sql);
                    $q->execute(
                        array(
                            ':tanggal'      => $tanggal->format('Y-m-d H:i:s'),
                            ':keyword'      => strtolower($_POST['kata-kunci']),
                            ':pertanyaan'   => strtolower($_POST['pertanyaan']),
                            ':hasil'        => $detailHasil,
                            ':jumlah_data'  => $jumlahCheck,
                        )
                    );
                }
                unset($_SESSION['keyword']);
            }

            $_SESSION['keyword'] = isset($_POST['keyword']) ? str_replace("'", "", $_POST['keyword']) : str_replace("'", "", $_SESSION['keyword']);
            
            if (isset($_SESSION['keyword'])) :
            ?>
            <div class="alert alert-success" style="margin-top:10px;"><?php echo $_SESSION['keyword']; ?></div>
            <?php
            endif;

            ?>
            <br/>
            <style type="text/css">
                .table thead th {
                    vertical-align: middle;
                }
                table td.ayat-arabic {
                    font-family: myFirstFont;
                    text-align: center;
                    font-size: 25px;
                    letter-spacing: 1px;
                    vertical-align: middle;
                    weight: 10%;
                    line-height:40px;
                }
                table td.ayat {
                    text-align: center;
                    vertical-align: middle;
                    weight: 10%;
                    line-height:40px;
                }
            </style>

            <?php 

            $preProses = new PreProcessing($pdo);
            $checklist = new Checklist($pdo);

            $stemmerFactory = new \Sastrawi\Stemmer\StemmerFactory();
            $stemmer        = $stemmerFactory->createStemmer();
            $kata = $stemmer->stem($_SESSION['keyword']); 
            $lastResult = $checklist->searchAyat($kata);
            
            // if (count($lastResult['data']) > 0): 
            $result = $lastResult['data'];
            if (count($result) > 0) :
            $listNamaSuratIndonesia = $lastResult['surat-indonesia'];
            $listNamaSuratArabic = $lastResult['surat-arabic'];

            ?>
            <form method="post" action="">
                <div class="alert alert-success" style="margin-top:10px;">Menampilkan <?php echo $lastResult['jumlah-data']; ?> hasil</div>
                <button type="submit" name="vote" class="btn" style="-moz-border-bottom-colors: none; -moz-border-left-colors: none;
                -moz-border-right-colors: none; -moz-border-top-colors: none; background-color: #f5f5f5; background-image: linear-gradient(to bottom, #ffffff, #e6e6e6);
                background-repeat: repeat-x; border-color: rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.25); border-image: none; border-radius: 15;
                border-style: solid; border-width: 1px; box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05); color: #333333; cursor: pointer; display: inline-block; font-size: 14px; line-height: 20px; margin-bottom: 0; text-align: center; text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
                vertical-align: middle; width:100%;">Vote</button>
                <div class="clear">&nbsp;</div>
                <div class="control-group" style="width: 100%;">
                    <label class="control-label" for="pertanyaan">Masukkan Untuk Pertanyaan Apa</label>
                    <div class="controls" style="display: inline; margin-left: 10px; width:80%;">
                        <input style="width: 70%; border-radius: 10px; vertical-align: middle; padding-left: 14px; padding-right: 14px; background-color: #ffffff;
                border: 1px solid #cccccc; box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset; transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s; font-size: 14px;
                height: 35px; line-height: 20px;" type="text" id="pertanyaan" name="pertanyaan" placeholder="Siapa penghuni neraka?" class="input-xxlarge search-query">
                    </div>
                </div>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th rowspan="3" class="head-table">No</th>
                        <th rowspan="3" class="head-table">Ayat</th>
                        <th rowspan="3" class="head-table">Relevan</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $listCheckbox = array();
                    $no = 1;
                    foreach ($result as $surat => $dataSurat) :
                        foreach ($dataSurat as $ayat => $dataAyat) :
                        ?>
                        <tr>
                            <td><?php echo $no; ?></td>
                            <td>
                            <?php
                                echo $dataAyat['text-indonesia'];
                                echo '('.$listNamaSuratIndonesia[$surat].' : '.$ayat.')';
                                $listCheckbox[$surat.'-'.$ayat] = $surat.'-'.$ayat;
                            ?>
                            </td>
                            <td>
                                <input type="checkbox" name="<?php echo $surat.'-'.$ayat; ?>" value="<?php echo $surat.'-'.$ayat; ?>">
                            </td>
                        </tr>
                        <?php
                        $no++;
                        endforeach;
                    endforeach;
                    ?>
                </tbody>
            </table>
            <input type="hidden" name="list-checkbox" value="<?php echo implode("&", $listCheckbox) ?>">
            <input type="hidden" name="kata-kunci" value="<?php echo $_SESSION['keyword'] ?>">
            <button type="submit" name="vote" class="btn" style="-moz-border-bottom-colors: none; -moz-border-left-colors: none;
                -moz-border-right-colors: none; -moz-border-top-colors: none; background-color: #f5f5f5; background-image: linear-gradient(to bottom, #ffffff, #e6e6e6);
                background-repeat: repeat-x; border-color: rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.25); border-image: none; border-radius: 15;
                border-style: solid; border-width: 1px; box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05); color: #333333; cursor: pointer; display: inline-block; font-size: 14px; line-height: 20px; margin-bottom: 0; text-align: center; text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
                vertical-align: middle; width:100%;">Vote</button>
            </form>
            <?php 
            endif; 
            ?>
        	<!-- CONTENT -->
        </div>
    </div>
    <!-- /.row -->
</div>
<!-- /.container-fluid -->